package com.smartjobs.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import java.sql.PreparedStatement;

public class DBManager {

	public static ResultSet resultSet = null;
	public static Connection connection = null;
	public static PreparedStatement preparedStatement = null;

	public static ResultSet selectFromDB(String query) {
		MyLog.log("Inside selectFromDB");
		MyLog.log("Query: " + query);
		connection = null;
		try {
			connection = DBConnection.getConnection();
			preparedStatement = connection.prepareStatement(query);
			resultSet = preparedStatement.executeQuery();
			return resultSet;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public static boolean insertIntoDB(String query) {
		MyLog.log("Inside insertIntoDB:DBManager");
		MyLog.log("Query: " + query);
		connection = null;
		try {
			connection = DBConnection.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.executeUpdate();
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}

	}

	public static boolean updateTable(String query) {
		MyLog.log("Inside updateTable");
		MyLog.log("Query: " + query);
		connection = null;
		try {
			connection = DBConnection.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.executeUpdate();
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}

	}

	public static boolean deleteFromDB(String query) {
		MyLog.log("Inside deleteFromDB");
		MyLog.log("Query: " + query);
		connection = null;
		try {
			connection = DBConnection.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.executeUpdate();
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}

	}

	public void closeConnection() throws SQLException {
		resultSet.close();
		preparedStatement.close();
		DBConnection db = new DBConnection();
		db.closeConnection();
	}
}
